<template>
    <div class="wrapper">
        <div class="loginbox">
            <div class="img">
                <img src="../assets/head.jpg" alt="">
            </div>
            <el-form :model="loginForm" :rules="loginFormRules" 
            ref="loginFormRef" label-width="0px" class="login_form">
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" prefix-icon="el-icon-user-solid"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="loginForm.password" type="password" prefix-icon="el-icon-s-goods"></el-input>
                </el-form-item>
                <el-form-item class="loginbtn">
                    <el-button type="primary" @click="login">登录</el-button>
                    <el-button type="info">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>

export default {
    name:"login",
    data(){
        return{
            loginForm:{
                "username":"",
                "password":""
            },
            loginFormRules: {
                username:[
                {required:true,message:'请输入登录名称'},
                {min:3,max:10,message:'长度在 3 到 10 个字符'}
                ],
                // 验证密码是否合法
                password: [
                {required:true,message:'请输入登录密码'},
                {min:6,max:15,message:'长度在 6 到 15 个字符'}
                ]
            }
        }
    },
    methods:{
        login(){
            console.log(this.loginForm)
            this.$http.post("login",this.loginForm)
            .then(res =>{
                console.log(res)
                if(res.data.meta.status==200){
                    this.$message.success('登录成功')
                    // 登陆成功后，将token保存到sessionStorage中
                    window.sessionStorage.setItem("token",res.data.data.token)
                    console.log("token:"+res.data.data.token)
                    this.$router.push('/home')
                }
                else{
                    this.$message.error('登录失败！')
                }
            })
            // axios({
            //     method:"post",
            //     url:"http://127.0.0.1:8888/api/private/v1/login",
            //     data:this.loginForm
            // }).then(res=>{
            //     console.log(res)
            // })
            // this.$router.push({path:'/home'})
        }
    }
}
</script>

<style scoped>
.wrapper{
    background-color: #2B4B6B;
    width: 100%;
    height: 100%;
}
.loginbox{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
}
.img{
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: white;
}
img{
    width: 100%;
    height: 100%;
    border-radius: 50%;
    /* background-color:white; */
}
.el-form{
    width: 90%;
    padding: 0 20px;
    margin-top: 110px;
}
.loginbtn{
    text-align: right;
}
</style>